<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\DemoRequest;
use App\Models\UserAsksFor;
use App\Models\UserBrowse;
use App\Models\UserCollecting;
use Illuminate\Http\Request;

class HouseController extends Controller
{
    /**
     * Author:Yingbo Sun
     * 用户求租/求购
     * @param DemoRequest $request
     * @return mixed
     */
    public function asksForHouse(DemoRequest $request)
    {
        $params = $request->validated();
        $params['uid'] = Request()->uid;
        $data = UserAsksFor::create($params);
        if ($data){
            return $this->success('添加成功');
        }else{
            return $this->fail('添加失败');
        }
    }

    /**
     * Author:Yingbo Sun
     * 求租/求购列表
     * @param Request $request
     * @return mixed
     */
    public function askforList(Request $request)
    {
        $type = $request->get('type',0);
        $province = $request->get('province',0);
        if (empty($province)){
            return  $this->fail('参数错误');
        }
        $data = UserAsksFor::where(['request_type'=>$type,'province'=>$province])->with('userAsksFor')->get();
        if ($data){
            $data = $data->toArray();
        }else{
            $data = [];
        }
        return $this->fail('获取成功',$data);
    }

    /**
     * Author:Yingbo Sun
     * 收藏房源
     * @param Request $request
     * @return mixed
     */
    public function collect(Request $request)
    {
        $uid = $request->uid;
        $fangId = $request->post('fang_id',0);
        $fangType = $request->post('fang_type',0);
        if (empty($uid) || empty($fangId)){
            return $this->fail('参数错误');
        }
        $res = UserCollecting::collecting($uid,$fangId,$fangType);
        if ($res){
            return $this->success('收藏成功');
        }else{
            return $this->fail('收藏失败');
        }
    }

    /**
     * Author:Yingbo Sun
     * 取消收藏
     * @param Request $request
     * @return mixed
     */
    public function cancelCollect(Request $request)
    {
        $uid = $request->uid;
        $fangId = $request->post('fang_id',0);
        $fangType = $request->post('fang_type',0);
        if (empty($uid) || empty($fangId)){
            return $this->fail('参数错误');
        }

        $res = UserCollecting::cancel($uid,$fangId);
        if ($res){
            return $this->success('取消成功');
        }else{
            return $this->fail('取消失败');
        }
    }

    public function isCollect(Request $request)
    {
        $uid = $request->uid;
        $fangId = $request->post('fang_id');
        $fangType = $request->post('fang_type',0);
        if (empty($uid) || empty($fangId)){
            return $this->fail('参数错误');
        }
        $res = UserCollecting::show($uid,$fangId);
        if (!empty($res)){
            return $this->success('点',true);
        }else{
            return $this->fail('没',false);
        }
    }

    //oClqt6lTlfyBs2kn9vAfU4aUhSec

    /**
     * Author:Yingbo Sun
     * 用户浏览记录
     * @param Request $request
     * @return mixed
     */
    public function browseHistory(Request $request)
    {
        $uid = $request->uid;
        $fangId = $request->post('fang_id',0);
        $fangType = $request->post('fang_type',0);
        if (empty($uid) || empty($fangId)){
            return $this->fail('参数错误');
        }
        $res = UserBrowse::queryHistory($uid,$fangId,$fangType);
        if (empty($res)){
            $data = UserBrowse::browseHistory($uid,$fangId,$fangType);
            if ($data){
                return $this->success('成功');
            }else{
                return $this->fail('失败');
            }
        }else{
            return $this->success('成功');
        }

    }

    /**
     * Author:Yingbo Sun
     * 浏览记录列表
     * @param Request $request
     * @return mixed
     */
    public function browseList(Request $request)
    {
        $uid = $request->uid;
        $fangType = $request->post('fang_type',0);
        if (empty($uid)){
            return self::fail('参数错误');
        }
        $data = UserBrowse::browseList($uid,$fangType);
        if ($data){
            $data = $data->toArray();
        }else{
            $data = [];
        }
        return $this->success('获取成功',$data);
    }


}
